<?php
class Members_IndexController extends Zendvn_Controller_Action{
	
	//Mang tham so nhan duoc o moi Action
	protected $_arrParam;
	
	//Duong dan cua Controller
	protected $_currentController;
	
	//Duong dan cua Action chinh
	protected $_actionMain;
	
	//Thong so phan trang
	protected $_paginator = array(
									'itemCountPerPage' => 15,
									'pageRange' => 4,
								  ); 
	protected $_namespace;
	
	public function init(){
		
		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();
		
		//Duong dan cua Controller
		$this->_currentController = '/' . $this->_arrParam['module'] 
									 . '/' . $this->_arrParam['controller'];		
		//Duong dan cua Action chinh		
		$this->_actionMain = '/' . $this->_arrParam['module'] 
							 . '/' . $this->_arrParam['controller']	. '/index';
							 
		$this->_paginator['currentPage'] = $this->_request->getParam('page',1);
		$this->_arrParam['paginator'] = $this->_paginator;
		
		//Luu cac du lieu filter vao SESSION
		//Dat ten SESSION
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		//$ssFilter->unsetAll();
		if(empty($ssFilter->col)){
			$ssFilter->keySearch 	= '';
			$ssFilter->lang 		= '';
		}
		$this->_arrParam['ssFilter']['keySearch'] 	= $ssFilter->keySearch;	
		$this->_arrParam['ssFilter']['lang'] 		= $this->_arrParam['lang'];	

		/*====================================================
		 * Load Zendvn Translate for Controller
		 *====================================================*/
		/*$this->_langObj->setLangFile(array('default.language.tmx'));		
		Zend_Registry::set('Zend_Translate', $this->_langObj->generate());*/
		
		$info = new Zendvn_System_Info();
		
		$language = $info->getLanguage('admin');
		
		
		if(empty($language)){
			$language = 'vi';
		}
		$translate = array(
							'adapter' => 'tmx',
							'content' => LANG_PATH . '/' . $language . '/admin/default.language.tmx',
							'locale' => $language,
							);
		$translate = new Zend_Translate($translate);
		Zend_Registry::set('Zend_Translate', $translate);
		
		//Truyen ra view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;		
		$template_path = TEMPLATE_PATH . "/public/system";
		$this->loadTemplate($template_path,'template.ini','template');		
	}
	public function indexAction(){
		$this->view->headTitle('Doanh nghiệp',true);		
	}	
	public function filterAction(){
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		if($this->_arrParam['type'] == 'search'){			
			if($this->_arrParam['key'] == 1){
				$ssFilter->keySearch = trim($this->_arrParam['keySearch']);
			}else{
				$ssFilter->keySearch = '';
			}
		}		
		$this->_redirect($this->_actionMain);
		$this->_helper->viewRenderer->setNoRender();
	}
	public function categoryAction(){	
		$tblNews = new Zendvn_Models_MembersItem();
		$this->view->Items = $tblNews->listItem($this->_arrParam,array('task'=>'public-category'));
		
		$tblcat = new Zendvn_Models_CategoryMembers();
		$cat = $tblcat->listItem($this->_arrParam,array('task'=>'find-category'));
		$this->view->Title = $cat['name'];
		$this->view->headTitle($this->view->Title,true);
		
		$totalItem  = $tblNews->countItem($this->_arrParam,array('task'=>'public-category'));
		$paginator = new Zendvn_Paginator();
		$this->view->panigator = $paginator->createPaginator($totalItem,$this->_paginator);
		$this->view->total = $tblNews->countItem($this->_arrParam,array('task'=>'public-category'));
	}
	public function detailAction(){		
		$tblDetail = new Zendvn_Models_MembersItem();
		$this->view->Item = $tblDetail->getItem($this->_arrParam,array('task'=>'public-detail'));
        $this->view->Cat = $tblDetail->listItem($this->_arrParam,array('task'=>'public-detail'));
		$this->view->Title = $this->view->Item['name'];
		$this->_arrParam['Item'] = $this->view->Item;
		$tblHits = $tblDetail->saveItem($this->_arrParam,array('task'=>'public-detail'));
		$this->view->headTitle($this->view->Title,true);
		$description = $this->view->Item['description_html'];
		$keywords	 = $this->view->Item['keywords_html'];
		$this->view->headMeta(true)->setName('description',$description);
		$this->view->headMeta(true)->setName('keywords',$keywords);
	}
	public function commentAction(){
		$tblComments = new Zendvn_Models_Comments();		
		if($this->_request->isPost()){
			$validator = new News_Form_ValidateComment($this->_arrParam);
			if($validator->isError() == true){
				$this->view->errors = $validator->getMessageError();
				$this->view->Item	= $validator->getData();
			}else{
				$arrParam = $validator->getData(array('upload'=>true));
				$tblComments->saveItem($arrParam,array('task'=>'add-comment'));
				//$this->_redirect($this->_currentController . '/success');
			}
		}
		$this->_helper->layout->disableLayout();		
	}
	public function successAction(){
		$this->_helper->layout->disableLayout();
	}
	public function exelAction(){
		$dir =  FILES_PATH . '/exels/';
		require_once $dir .'excel_reader2.php';
		$data = new Spreadsheet_Excel_Reader($dir . "test2.xls");
		$files = $data->sheets[0]['cells'];
		unset($files[1]);
		$this->_arrParam['files'] = $files;
		$tblExel = new Zendvn_Models_Exels();
		$list = $tblExel->saveItem($this->_arrParam, array('task' => 'admin-add'));
	}
	public function importExelAction(){
		$tblExel = new Zendvn_Models_Exels();
		if($this->_request->isPost()){
			$validator = new News_Form_ValidateExel($this->_arrParam);
			if($validator->isError() == true){
				$this->view->errors = $validator->getMessageError();
				$this->view->Item	= $validator->getData();
				
			}else{				
				$arrParam = $validator->getData(array('upload'=>true));
				$filesname = $arrParam['fileexel'];
				$dir =  FILES_PATH . '/exels/';
				require_once $dir .'excel_reader2.php';
				$data = new Spreadsheet_Excel_Reader($dir . $filesname);
				$files = $data->sheets[0]['cells'];		
				unset($files[1]);
				$arrParam['files'] = $files;
				$tblExel = new Zendvn_Models_Exels();
				$list = $tblExel->saveItem2($arrParam, array('task' => 'admin-add'));
				$tblDelete = $tblExel->delete($arrParam,array('task'=>'add-delete'));
				//$this->_redirect($this->_currentController . '/success');
			}
		}
		//$this->_helper->layout->disableLayout();
	}
	
	public function advertAction(){		
		$tblAdvert = new Zendvn_Models_Advert();
		//$this->view->Item = $tblAdvert->getItem($this->_arrParam,array('task'=>'public-advert'));
		$this->view->Item = $tblAdvert->listItem($this->_arrParam,array('task'=>'public-advert'));
		$this->view->Title = $this->view->Item[0]['name'];
		$this->view->headTitle($this->view->Title,true);
		//$this->view->headMeta(true)->setName('description',$this->view->Item['name']);
	}
	public function MembersAction(){
	   	$this->view->Title = 'Doanh nghiệp';
	}
	public function MembersCatAction(){
		$type = $this->_arrParam['type'];
		if($type == 1){
			$this->view->headTitle('Doanh nghiệp tiêu biểu',true);
		}elseif ($type == 2){
			$this->view->headTitle('Doanh nghiệp tiềm năng',true);
		}elseif ($type == 3){
			$this->view->headTitle('Doanh nghiệp mới',true);
		}
		$tblMembers = new Zendvn_Models_MembersItem(); 
		$items = $tblMembers->listItem($this->_arrParam,array('task'=>'public-category'));
	}
	public function uploadExelAction(){
		
	}
	public function newNewsAction(){
		
	}
	public function menuAction(){
		
	}
}